package com.util.format;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author qiujunda
 * @date 2019年3月6日
 * @description
 */
public class DateFormatUtil {
    public static int getMinute(Date before, Date after) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        long time1 = before.getTime();
        long time2 = after.getTime();
        long diff;
        diff = time2 - time1;
        day = diff / (24 * 60 * 60 * 1000);
        hour = (diff / (60 * 60 * 1000) - day * 24);
        min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
        sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        System.out.println(day + "天" + hour + "小时" + min + "分" + sec + "秒");
        return new Long(day * 24 * 60 + hour * 60 + min).intValue();
    }

    public static String getCurrentTime() {
        Date d = new Date();
        return formatTime(d);
    }

    public static String formatIdPre(Date date) {
        if (date == null)
            return "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmm");
        return sdf.format(date);
    }

    public static String format(Date date) {
        if (date == null)
            return "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    public static String formatOnlyTime(Date date) {
        if (date == null)
            return "";
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return sdf.format(date);
    }

    public static String formatTransferConfig(Date date) {
        if (date == null)
            return "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(date);
    }

    public static Date parseDate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date returnDate;
        try {
            returnDate = (Date) sdf.parse(date);
        } catch (ParseException e) {
            return null;
        }
        return returnDate;
    }

    public static String format(Timestamp timestamp) {
        if (timestamp == null)
            return "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(timestamp);
    }

    public static String formatTime(Date date) {
        if (date == null)
            return "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static String formatTimeNoSs(Date date) {
        if (date == null)
            return "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.format(date);
    }

    public static String formatTimeChina(Date date) {
        if (date == null)
            return "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH点");
        return sdf.format(date);
    }

    public static Timestamp parse(String timestamp) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return (Timestamp) sdf.parse(timestamp);
    }

    public static Date parseNoSs(String dateString) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sdf.parse(dateString);
    }

    public static Date parseDateTime(String timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(timestamp);
        } catch (ParseException e) {
            return null;
        }
    }


    public static List<String> getDateString() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date afterDate = new Date(date.getTime() + 65000);
        List<String> stringList = new ArrayList<>();
        stringList.add(sdf.format(date));
        stringList.add(sdf.format(afterDate));
        return stringList;
    }

    public static Date getTheDateBefore(Date date, int d) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, d);
        return calendar.getTime();
    }

    public static String getTheDateStrBefore(Date date, int d) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, d);
        return format(calendar.getTime());
    }

    public static Date getTheMinBefore(Date date, int d) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, d);
        return calendar.getTime();
    }

    public static String getTheMinStrBefore(Date date, int d) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, d);
        return format(calendar.getTime());
    }

    /**
     * 获取本月最后一天日期
     *
     * @return
     */
    public static Date getTheMonthLastDate() {
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        String lastDate = format(cale.getTime());
        lastDate += " 23:59:59";
        try {
            Date d = parseDateTime(lastDate);
            return d;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cale.getTime();
    }

    /**
     * 获取本月第一天日期
     *
     * @return
     */
    public static Date getTheMonthFirstDate() {
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        String lastDate = format(cale.getTime());
        lastDate += " 00:00:00";
        try {
            Date d = parseDateTime(lastDate);
            return d;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cale.getTime();
    }

    /**
     * 获取上月最后一天日期
     *
     * @return
     */
    public static Date getPreMonthLastDate() {
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        String lastDate = format(cale.getTime());
        lastDate += " 23:59:59";
        try {
            Date d = parseDateTime(lastDate);
            return d;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cale.getTime();
    }

    /**
     * 获取上月第一天日期
     *
     * @return
     */
    public static Date getPreMonthFirstDate() {
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, -1);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        String lastDate = format(cale.getTime());
        lastDate += " 00:00:00";
        try {
            Date d = parseDateTime(lastDate);
            return d;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cale.getTime();
    }

    /**
     * 获取该时间当月第一天
     *
     * @return
     */
    public static Date getTheDayFirstDate(Date date) {
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        String lastDate = format(cale.getTime());
        lastDate += " 00:00:00";
        try {
            Date d = parseDateTime(lastDate);
            return d;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cale.getTime();
    }

    /**
     * 获取该时间当月第一天
     *
     * @return
     */
    public static Date getTheDayLastDate(Date date) {
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        String lastDate = format(cale.getTime());
        lastDate += " 23:59:59";
        try {
            Date d = parseDateTime(lastDate);
            return d;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cale.getTime();
    }

    /**
     * 获取星期几：1 星期一，2 星期二
     *
     * @param date
     * @return
     */
    public static int getWeekday(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        return dayOfWeek - 1;
    }
    /**
     * 获取两个日期之间的所有日期
     *
     * @param startTime
     *            开始日期
     * @param endTime
     *            结束日期
     * @return
     */
    public static List<String> getDays(String startTime, String endTime) {

        // 返回的日期集合
        List<String> days = new ArrayList<String>();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return days;
    }

    /**
     * 获取两个日期之间的所有日期
     *
     * @param startTime
     *            开始日期
     * @param endTime
     *            结束日期
     * @return
     */
    public static List<String> getDays(Date startTime, Date endTime) {

        // 返回的日期集合
        List<String> days = new ArrayList<String>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(startTime);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(endTime);
        tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
        while (tempStart.before(tempEnd)) {
            days.add(dateFormat.format(tempStart.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return days;
    }

    /**
     * 将日期转化成相应格式的字符串
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 获取两个日期相隔的天数
     * @param before
     * @param after
     * @return
     */
    public static int getDaySpace(Date before, Date after) {
        return (int)((after.getTime() - before.getTime()) / (24 * 60 * 60 * 1000));
    }

    /**
     * 获取两个日期相隔的分钟数
     * @param before
     * @param after
     * @return
     */
    public static long getMinuteSpace(Date before, Date after) {
        return ((after.getTime() - before.getTime()) / (60 * 1000));
    }

    /**
     * 获取某时间加上X小时之后的日期
     * @param date
     * @param hour
     * @return
     */
    public static Date getDateAddHour(Date date, int hour) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.HOUR_OF_DAY, hour);
            return calendar.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] arsg) {
        System.out.println(getTheDateStrBefore(new Date(), 1));
    }
}
